//
//  KCCTVViewController.m
//  Keping
//
//  Created by 柯平 on 2017/6/30.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KCCTVViewController.h"
#import "CCTVSettingViewController.h"
#import "KCCTVRequest.h"
#import "KCCTVsData.h"
#import "KCCTVContentView.h"

@interface KCCTVViewController ()<KCCTVContentViewDelegate>

@property(nonatomic,assign)KCCTVShowMode showMode;
@property(nonatomic,strong)KCCTVContentView* cctvView;
@property(nonatomic,strong)KLiveModeView* modeView;
@property(nonatomic,strong)KCCTVsData* cctvData;
@end

@implementation KCCTVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUI];
    [self getData];
    _showMode = KCCTVShowModeOne;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

-(void)getData
{
    [super getData];
    [self showLoading:@"Loading..."];
    KCCTVRequest* request = [[KCCTVRequest alloc] init];
    WEAKSELF
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        KCCTVsData* cctvData = [KCCTVsData modelWithJSON:request.responseData];
        weakSelf.cctvData = cctvData;
        NSArray *array = cctvData.cctvEntityViews;
        NSMutableArray *entityArr = [NSMutableArray new];
        for (int i = 0; i < array.count; i++) {
            KCctvEntityView *entity = [array objectAtIndex:i];
//            if ([entity.status isEqualToString:@"Y"]) {
                [entityArr addObject:entity];
//            }
        }
        weakSelf.cctvView.cctvEntityViews = [entityArr copy];
        if (cctvData.statusCode != 200) {
            [weakSelf showErrorText:cctvData.msg errCode:cctvData.statusCode];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        [weakSelf showErrorText:
         request.error.localizedDescription?:@"Network Error!"];
    }];
}

#pragma mark KCCTVContentViewDelegate 点击进入CCTV的详情页面播放
- (void)contentViewdidSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(void)setUI
{
    self.navigationItem.title=@"CCTV";
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"cctv_enum.png"] style:UIBarButtonItemStylePlain target:self action:@selector(enumAction)];
    
    self.view.backgroundColor = KBackColor;
    
    _cctvView = [KCCTVContentView new];
    _cctvView.delegate = self;
    _cctvView.backgroundColor = kBlackColor;
    [self.view addSubview:_cctvView];

    _modeView = [KLiveModeView new];
    _modeView.backgroundColor = kWhiteColor;
    [self.view addSubview:_modeView];
    
    _cctvView.top = 0;
    _cctvView.left = 0;
    _modeView.top = _cctvView.bottom;
    _modeView.left = _cctvView.left;
}


-(void)enumAction
{
    CCTVSettingViewController* vc = [CCTVSettingViewController new];
    vc.cctvEntityViews = self.cctvData.cctvEntityViews;
    WEAKSELF
    vc.updateCCTVDatas = ^{
        [weakSelf getData];
    };
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
